﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AP.Framework.Infrastructure.Repositories.IRepositories;
using AP.Framework.Infrastructure.Entities;
using AP.Framework.Utilities.MvcPager;
namespace ApCMS.Areas.Admin.Controllers
{
    public class UsersController : Controller
    {
        IUsers _user;
        public UsersController(IUsers user)
        {
            _user = user;
        }
        public ActionResult Index(int? page, string SearchValue)
        {
            var userList = _user.GetAll();
            if (!string.IsNullOrWhiteSpace(SearchValue))
            {
                userList = userList.Where(p => p.UserName.Contains(SearchValue));
            }
            return View(userList.AsQueryable().ToPagedList(page ?? 1, 10));
        }
        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Add(sys_Users model)
        {
            if (ModelState.IsValid)
            {

                model.CreateTime = DateTime.Now.Date;
                _user.Add(model);
            }
            return RedirectToAction("Index");
        }
        public ActionResult Edit(int userId)
        {
            sys_Users model = _user.FindById(userId);
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(sys_Users model)
        {
            if (ModelState.IsValid)
            {
                model.CreateTime = DateTime.Now.Date;
                _user.Modify(model);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Delete(int userId)
        {
            sys_Users model = _user.FindById(userId);
            _user.Remove(model);
            return RedirectToAction("Index");
        }

    }
}
